﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

using HanhuaMicroCredit.BLL;
using Library.Common;
using Library.Bll;

namespace HanhuaMicroCredit.Admin.Controllers
{
    public class SysController : UserAuthenController
    {
        //
        // GET: /Sys/

        public ActionResult content(int id)
        {

            List<Dictionary<string, object>> model = NoticeBLL.getDic(id);
            //ViewData["id"] = id;
            return View("~/Views/Sys/Notice/content.ascx", model);
        }

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult DataList()
        {
            return View("~/Views/Shared/DataList.aspx");
        }

        private string getR(string oper, string menu)
        {
            string r = "~/Views/Sys/{0}/{1}.aspx";
            return string.Format(r, menu, oper);
        }

        #region //页面规范
        public ActionResult Add(string menu)
        {
            return View(getR("Add", menu));
        }
        public ActionResult AddNode(string menu, string tname, int id, string name)
        {
            //Dictionary<string, object> d = CommonBLL.GetModel(tname, "*", id);
            tname = DencryptHelper.Decrypt(tname);
            ViewData["id"] = id;
            ViewData["name"] = selectControlBLL.getNameById("id", id.ToString(), tname, name);
            return View(getR("AddNode", menu));
        }
        public ActionResult Edit(string menu, string tname, int id)
        {
            Dictionary<string, object> d = CommonBLL.GetModel(tname, "*", id);
            //
            ViewData["id"] = id;
            return View(getR("Edit", menu), d);
        }
        public ActionResult List(string menu)
        {
            return View(getR("List", menu));
        }

        #endregion

        public ActionResult UserList()
        {
            return View();
        }
        public ActionResult UserAdd()
        {
            return View();
        }
        public ActionResult MenuConfig(int id, string tname, string m, string updateId, string d)
        {
            int td = Utils.StrToInt(d, 0);
            if (td != 0)
                td = td - 1;
            int maxVal = 0;
            bool more = m == "all" ? true : false;
            ViewData["more"] = more;
            ViewData["listDt"] = getChildMenuById(id, tname, more, 1, out maxVal, td);
            string url = "/Sys/MenuConfig/" + id;
            url += "/?tname=" + tname;
            if (!more)
                url += "&m=all";
            if (td != 0)
                url += "&d=" + d;
            ViewData["Open"] = string.Format("<a href=\"{0}\">{1}</a>", url, more ? "收起" : "展开");
            ViewData["Order"] = maxVal + 1;
            ViewData["id"] = id;
            //修改
            int uId = Utils.StrToInt(MyRequest.GetQueryString("updateId"), 0);
            ViewData["uId"] = uId;
            if (uId != 0)
            {
                menuConfig menu = menuConfig.Instance;
                menu._tname = tname;
                menu._id = uId;
                menu._dtnum = td;
                ViewData["UpdateDt"] = menu.G;
            }
            return View();
        }
        private string getChildMenuById(int id, string tname, bool more, int i, out int maxVal, int td)
        {
            menuConfig m = menuConfig.Instance;
            m._tname = tname;
            m._pid = id;
            m._dtnum = td;
            List<Dictionary<string, object>> l = m.S;

            StringBuilder sbHtml = new StringBuilder();
            if (i == 1)
            {
                string u = "";
                if (more)
                    u += "&m=all";
                u += "&d=" + (td + 1);
                sbHtml.Append("<li>");
                sbHtml.Append(string.Format("<a href=\"{0}\">{1}</a>", "/Sys/MenuConfig/0/?tname=" + tname + u, "根节点"));
                sbHtml.Append("</li>");
                if (id != 0)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append(Utils.GetSpacesString(2, "&nbsp"));
                    sbHtml.Append(string.Format("<a href=\"{0}\">{1}</a>", "/Sys/MenuConfig/" + id + "/?tname=" + tname + u, selectControlBLL.getNameById(td, "id", id.ToString(), tname, "title")));
                    sbHtml.Append("</li>");
                }
            }
            int p = 0;
            foreach (Dictionary<string, object> d in l)
            {
                int ids = Utils.StrToInt(d["id"], 0);
                string imgUrl = Utils.GetSpacesString(2, "&nbsp") + "├" + Utils.GetSpacesString(i, "─");
                string url = "/Sys/MenuConfig/" + ids;
                url += "/?tname=" + tname;
                url += "&d=" + (td + 1);
                if (more)
                    url += "&m=all";
                url = string.Format("<a href=\"{0}\">{1}</a>", url, d["title"]);
                sbHtml.Append("<li>");
                sbHtml.Append(imgUrl + url);
                //操作
                string operate = Utils.GetSpacesString(1, "&nbsp");
                if (!more)
                {
                    operate += p + 1;
                    operate += Utils.GetSpacesString(1, "&nbsp");
                    if (p != 0)
                        operate += string.Format("<a href=\"{0}\">{1}</a>", "#", "↑");
                    else
                        operate += "↑";
                    operate += Utils.GetSpacesString(1, "&nbsp");
                    if (p != l.Count - 1)
                        operate += string.Format("<a href=\"{0}\">{1}</a>", "#", "↓");
                    else
                        operate += "↓";
                    operate += Utils.GetSpacesString(1, "&nbsp");
                }
                string retUrl = "/Sys/MenuConfig/" + id + "/?tname=" + tname + "&updateid=" + ids;
                retUrl += "&d=" + (td + 1);
                operate += string.Format("<a href=\"{0}\">{1}</a>", retUrl, "修改");
                //operate += Utils.GetSpacesString(1, "&nbsp");
                //operate += string.Format("<a href=\"{0}\">{1}</a>", "#", "移动");
                operate += Utils.GetSpacesString(1, "&nbsp");

                operate += JUIResult.ReturnDel(ids.ToString(), tname, retUrl, "1");
                sbHtml.Append(operate);
                sbHtml.Append("</li>");
                int max = 0;
                if (more)
                    sbHtml.Append(getChildMenuById(ids, tname, more, i + 1, out max, td));
                p++;
            }
            maxVal = l.Count;
            return sbHtml.ToString();
        }

        public ActionResult EditPhones(int id)
        {
            string tableName = "Set_400Phones";
            string selectColumns = "*";
            Dictionary<string, object> dic = CommonBLL.GetModel(Database.Web, tableName, selectColumns, id);
            //
            ViewData["id"] = id;
            return View(dic);
        }
        public ActionResult ListPhones()
        {
            return View();
        }

        #region IP管理
        private const string IpTname = "Set_IpRouteSites";
        private const string IpNumStart = "t0_IpNumStart";
        private const string IpNumEnd = "t0_IpNumEnd";
        public ActionResult EditIP(string menu, int id)
        {
            Dictionary<string, object> d = CommonBLL.GetModel(Database.Web, IpTname, "*", id);
            //
            ViewData["id"] = id;
            return View(getR("EditIP", menu), d);
        }
        /// <summary>
        /// 修改IP信息
        /// </summary>
        /// <param name="url"></param>
        /// <param name="fc"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditIP(string url, FormCollection fc, int id)
        {
            fc = AddNum(fc);
            OperateTotalController otc = new OperateTotalController();
            return otc.Edit(3, id, DencryptHelper.Encrypt(IpTname), url, fc, "0");

        }
        public ActionResult AddIp(string menu)
        {
            return View(getR("AddIp", menu));
        }
        /// <summary>
        /// 添加IP
        /// </summary>
        /// <param name="url"></param>
        /// <param name="fc"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddIp(string url, FormCollection fc)
        {
            fc = AddNum(fc);
            OperateTotalController otc = new OperateTotalController();
            return otc.Add(3, DencryptHelper.Encrypt(IpTname), url, fc);
        }
        /// <summary>
        /// 向集合中添加IP地址生成的数字
        /// </summary>
        /// <param name="fc">数据集合</param>
        /// <returns>FormCollection</returns>
        private FormCollection AddNum(FormCollection fc)
        {
            string sNum = SetIpRouteSites.IpToNumber(fc["t0_IpAddressStart"].ToString()).ToString();
            string eNum = SetIpRouteSites.IpToNumber(fc["t0_IpAddressEnd"].ToString()).ToString();
            fc.Add(IpNumStart, sNum);
            fc.Add(IpNumEnd, eNum);
            return fc;
        }
        #endregion
    }
}
